'''
#Coding: utf-8
#Project: online-exam
#Author: rtf
#Time: 2022-08-02 18:39:10
#FileName: student_score.py
#Softwar: PyCharm
'''


import json
import random
from trump.query import get_item, get_items, get_table_header, modify_item
from trump.utils import ok, fail
from trump.decorators import table_headers
from utils.params_validate_decorator import validate_params
from funcs.user_info import get_uesr_info
from trump.utils import get_user

__table__ = "student_exam"


async def ls(app, request):
    params = request.args
    user = get_user(request)
    role = user.get("role")
    params["status"] = (1,)
    if role == "TEACHER":
        params["teacher_no"] = (user.get("teacher_no"),)
        params["mark_status"] = (0,)
    elif role == "STUDENT":
        params["id"] = (-1, )
    else:
        params["mark_status"] = (0,)
    print("======", params)


async def post_ls(app, request, data):
    for x in data:
        exam_paper = await get_item(app.ctx.pool, "exam_paper", x.get("exam_paper_id"))
        student_answer = await get_item(app.ctx.pool, "student_answer", x.get("id"), column="student_exam_id")
        student_answer = json.loads(student_answer.get("student_answer"))
        exam_questions = json.loads(exam_paper.get("exam_questions"))
        questions = list()
        for q in exam_questions:
            if "问答题" in q.get("title"):
                questions = q.get("questions")
        new_questions = list()
        for q in questions:
            new_questions.append({"no": q.get("no"),
                                  "subject": q.get("subject"),
                                  "answer": student_answer.get(str(q.get("no"))),
                                  "score": q.get("score")})
        x["questions"] = new_questions
        exam_paper = await get_items(app.ctx.pool, "exam_paper")
        course = await get_items(app.ctx.pool, "course")

        for e in exam_paper:
            if x.get("exam_paper_id") == e.get("id"):
                x["exam_paper_name"] = e.get("name")
                for i in course:
                    if e.get("course_code") == i.get("code"):
                        x["course_name"] = i.get("name")

        student = await get_item(app.ctx.pool, "student", x.get("student_no"), column="student_no")
        x["student_name"] = student.get("name")

        teacher = await get_item(app.ctx.pool, "teacher", x.get("teacher_no"), column="teacher_no")
        x["teacher_name"] = teacher.get("name")


async def get(app, request, id):
    pass


async def post_get(app, request, data, id):
    exam_paper = await get_item(app.ctx.pool, "exam_paper", data.get("exam_paper_id"))
    student_answer = await get_item(app.ctx.pool, "student_answer", data.get("id"), column="student_exam_id")
    student_answer = json.loads(student_answer.get("student_answer"))
    exam_questions = json.loads(exam_paper.get("exam_questions"))
    questions = list()
    for q in exam_questions:
        if "问答题" in q.get("title"):
            questions = q.get("questions")
    new_questions = list()
    for q in questions:
        new_questions.append({"no": q.get("no"),
                              "subject": q.get("subject"),
                              "answer": student_answer.get(str(q.get("no"))),
                              "score": q.get("score")})
    data["questions"] = new_questions
    exam_paper = await get_items(app.ctx.pool, "exam_paper")
    course = await get_items(app.ctx.pool, "course")

    for e in exam_paper:
        if data.get("exam_paper_id") == e.get("id"):
            data["exam_paper_name"] = e.get("name")
            for i in course:
                if e.get("course_code") == i.get("code"):
                    data["course_name"] = i.get("name")

    student = await get_item(app.ctx.pool, "student", data.get("student_no"), column="student_no")
    data["student_name"] = student.get("name")

    teacher = await get_item(app.ctx.pool, "teacher", data.get("teacher_no"), column="teacher_no")
    data["teacher_name"] = teacher.get("name")


async def post(app, request):
    params = request.json
    print("=======", params)

    student_exam_id = params.get("student_exam_id")
    score = params.get("score")

    await modify_item(app.ctx.pool, "student_exam", student_exam_id, {"mark_status": 1})
    student_score = await get_item(app.ctx.pool, "student_score", student_exam_id, column="student_exam_id")

    data = {"questions_score": score, "score": score + student_score.get("score")}
    await modify_item(app.ctx.pool, "student_score", student_score.get("id"), data)

    return ok()